<template>
  <div id="app">
    <!-- 全局加载状态 -->
    <div v-if="isLoading" class="app-loading">
      <div class="loading-content">
        <el-icon class="loading-icon" :size="50">
          <Loading />
        </el-icon>
        <p>正在初始化...</p>
      </div>
    </div>
    
    <!-- 主要内容区域 -->
    <router-view v-else />

    <!-- 开发环境API状态检查器 -->
    <ApiStatusChecker v-if="isDev" />
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { useUserStore } from '@/stores/user'
import { useRouter } from 'vue-router'
import { Loading } from '@element-plus/icons-vue'
import ApiStatusChecker from '@/components/dev/ApiStatusChecker.vue'

const userStore = useUserStore()
const router = useRouter()
const isLoading = ref(true)

// 开发环境检查
const isDev = import.meta.env.DEV

// 应用初始化
onMounted(async () => {
  try {
    // 从本地存储恢复用户偏好设置
    const storedPreferences = localStorage.getItem('userPreferences')
    if (storedPreferences) {
      try {
        userStore.preferences = {
          ...userStore.preferences,
          ...JSON.parse(storedPreferences)
        }
      } catch (error) {
        console.error('解析用户偏好失败:', error)
      }
    }
  } catch (error) {
    console.error('应用初始化过程中发生意外错误:', error)
  } finally {
    // 显示页面
    setTimeout(() => {
      isLoading.value = false
    }, 200)
  }
})
</script>

<style>
/* 全局样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
    Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background-color: #f5f5f5;
  color: #333;
}

#app {
  width: 100%;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 应用加载状态 */
.app-loading {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.loading-content {
  text-align: center;
  color: white;
}

.loading-icon {
  animation: spin 1s linear infinite;
  margin-bottom: 20px;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.loading-content p {
  font-size: 16px;
  font-weight: 500;
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  html {
    font-size: 16px;
  }
}

/* 链接样式 */
a {
  text-decoration: none;
  color: inherit;
}
</style>